package com.imooc.spikesystem.util;

import org.springframework.util.DigestUtils;

/**
 * @author wy
 */
public class MD5Util {
    public static String md5(String src){
        return DigestUtils.md5DigestAsHex(src.getBytes());
    }

    private static final String SALT = "1a2b3c4d";

    /**
     * 将前端传入的密码先经过MD5和字典数据打乱加密
     * @param inputPassword
     * @return
     */
    public static String intputPwdFromPass(String inputPassword){
        String str = "" + SALT.charAt(0) + SALT.charAt(2) + inputPassword + SALT.charAt(5);
        return md5(str);
    }

    /**
     * 将第一次加密的数据再和经过随机加密的数据。再加密，存入DB中
     * @param inputPassword
     * @param salt
     * @return
     */
    public static String fromPassToDBPass(String inputPassword,String salt){
        String str = "" + SALT.charAt(0) + SALT.charAt(2) + inputPassword + SALT.charAt(5);
        return md5(str);
    }

    /**
     * 综合两个方法，直接将前端输入密码双重加密之后存入DB中
     * @param input
     * @param saltDB
     * @return
     */
    public static String inputPassToDBPass(String input,String saltDB){
        String inputPass = intputPwdFromPass(input);
        String dbPass = fromPassToDBPass(inputPass, saltDB);
        return dbPass;
    }

/*  public static void main(String[] args) {
    System.out.println(inputPassToDBPass("123456","dictionary"));
  }*/
}
